home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / HAMRADIO / NOISE.ZIP / COMPLEX.C next >
C/C++ Source or Header  |  1993-08-08  |  5KB  |  226 lines

  1. /* complex.c - collection of routines to do imaginair and polar
  2.  * calculations.
  3.  *
  4.  * 18-Sep-1991
  5.  * Peter Beyer -PA3AEF-
  6.  * beyer@athena.uto.dec.com
  7.  */
  8. #ifndef _COMPLEXC_           /* Make it safe to include this twice */
  9. #define _COMPLEXC_
  10.  
  11. #ifndef __TURBOC__
  12. struct complex {
  13.     double x;                      /* Real part */
  14.     double y;                      /* Imaginair part */
  15. };
  16. #endif
  17.  
  18. struct polair {
  19.     double m;                      /* Magnitude */
  20.     double a;                      /* Angle in degrees */
  21. };
  22.  
  23. #include <math.h>
  24. #define RTOD(x)  ((x) * 57.29578)  /* Radians to Degrees */
  25. #define DTOR(x)  ((x) / 57.29578)  /* Degrees to Radians */
  26.  
  27. #ifdef _PROTO_
  28. void cadd (struct complex *a, struct complex *b, struct complex *z);
  29. void csub (struct complex *a, struct complex *b, struct complex *z);
  30. void cmul (struct complex *a, struct complex *b, struct complex *z);
  31. void cinv (struct complex *a, struct complex *z);
  32. void cdiv (struct complex *a, struct complex *b, struct complex *z);
  33. void csqrt (struct complex *a, struct complex *z);
  34. void c2pol (struct complex *a, struct polair *z);
  35. void pol2c (struct polair *a, struct complex *z);
  36. void poladd (struct polair *a, struct polair *b, struct polair *z);
  37. void polsub (struct polair *a, struct polair *b, struct polair *z);
  38. void polmul (struct polair *a, struct polair *b, struct polair *z);
  39. void polinv (struct polair *a, struct polair *z);
  40. void poldiv (struct polair *a, struct polair *b, struct polair *z);
  41. void polsqrt (struct polair *a, struct polair *z);
  42. #endif _PROTO_
  43.  
  44. /* Complex add function.
  45.  * Z = A + B
  46.  */
  47. void cadd(a, b, z)
  48. struct complex *a, *b, *z;
  49. {
  50.     z->x = a->x + b->x;
  51.     z->y = a->y + b->y;
  52. }
  53.  
  54. /* Complex substract function.
  55.  * Z = A - B
  56.  */
  57. void csub(a, b, z)
  58. struct complex *a, *b, *z;
  59. {
  60.     z->x = a->x - b->x;
  61.     z->y = a->y - b->y;
  62. }
  63.  
  64. /* Complex multiply function. 
  65.  * Z = A x B
  66.  */
  67. void cmul(a, b, z)
  68. struct complex *a, *b, *z;
  69. {
  70.     z->x = a->x * b->x - a->y * b->y;
  71.     z->y = a->x * b->y + a->y * b->x;
  72. }
  73.  
  74. /* Complex invert function
  75.  * Z = 1 / A
  76.  */
  77. void cinv(a, z)
  78. struct complex *a, *z;
  79. {
  80.     double p = a->x * a->x + a->y * a->y;
  81.     
  82.     z->x = a->x / p;
  83.     z->y = (-1 * (a->y) ) / p;
  84.     
  85. }
  86.  
  87. /* Complex devide function
  88.  * Z = A / B
  89.  */
  90. void cdiv(a, b, z)
  91. struct complex *a, *b, *z;
  92. {
  93.     struct complex p;
  94.     cinv(b,&p);
  95.     cmul(a,&p,z);
  96. }
  97.  
  98. /* Complex SQRT function
  99.  * Z = SQRT(A)
  100.  */
  101. void csqrt(a, z)
  102. struct complex *a, *z;
  103. {
  104.     double p;
  105.  
  106.     if ((a->x == 0) && (a->y == 0)) {
  107.         z = a;
  108.     } else {
  109.         p = sqrt((fabs(a->x) + cabs(*a)) / 2);
  110.         if ((a->x < 0 ) && (a->y < 0 ))
  111.             p = p * -1;
  112.         if (a->x < 0) {
  113.             z->y = p;
  114.             z->x = (a->x) / (2 * p);
  115.         } else {
  116.             z->x = p;
  117.             z->y = (a->y) / (2 * p);
  118.         }
  119.  
  120.     }
  121.  
  122. }
  123.  
  124. /* Convert rectangle coordiantes to polar
  125.  * coordinates. (Real, Imaginair to Magnitude, Angle)
  126.  * (Angle returns in degrees)
  127.  * Z = A;
  128.  */
  129. void c2pol(a, z)
  130. struct complex *a;
  131. struct polair *z;
  132. {
  133.     z->m = sqrt(a->x * a->x + a->y * a->y);
  134.     z->a = atan(a->y / a->x);
  135.     if ((a->x < 0) && (a->y < 0))
  136.         z->a -= M_PI;
  137.     if ((a->x < 0) && (a->y > 0))
  138.     z->a += M_PI;
  139.     z->a = RTOD(z->a);
  140. }
  141.  
  142. /* Convert polar coordiantes to rectangle coordiantes.
  143.  * (Magnitude, Angle to Real, Imaginair)
  144.  * Angle must be degrees
  145.  * Z = A
  146.  */
  147. void pol2c(a, z)
  148. struct polair *a;
  149. struct complex *z;
  150. {
  151.     z->x = a->m * cos(DTOR(a->a));
  152.     z->y = a->m * sin(DTOR(a->a));
  153. }
  154.  
  155. /* Polair Add function
  156.  * Z = A + B
  157.  */
  158. void poladd(a, b, z)
  159. struct polair *a, *b, *z;
  160. {
  161. struct complex p, q, r;
  162.     pol2c(a, &p);
  163.     pol2c(b, &q);
  164.     cadd(&p, &q, &r);
  165.     c2pol(&r, z);
  166. }
  167.  
  168. /* Polair Substract function
  169.  * Z = A - B
  170.  */
  171. void polsub(a, b, z)
  172. struct polair *a, *b, *z;
  173. {
  174. struct complex p, q, r;
  175.     pol2c(a, &p);
  176.     pol2c(b, &q);
  177.     csub(&p, &q, &r);
  178.     c2pol(&r, z);
  179. }
  180.  
  181. /* Polair Multiply
  182.  * Z = A * B
  183.  */
  184. void polmul(a, b, z)
  185. struct polair *a, *b, *z;
  186. {
  187.     z->m = a->m * b->m;
  188.     z->a = DTOR(a->a) + DTOR(b->a);
  189.     z->a = RTOD(z->a);
  190. }
  191.  
  192. /* Polair Devide function
  193.  * Z = A / B
  194.  */
  195. void poldiv(a, b, z)
  196. struct polair *a, *b, *z;
  197. {
  198.     z->m = a->m / b->m;
  199.     z->a = DTOR(a->a) - DTOR(b->a);
  200.     z->a = RTOD(z->a);
  201. }
  202.  
  203. /* Polair Invert function
  204.  * Z = 1 / A
  205.  */
  206. void polinv(a, z)
  207. struct polair *a, *z;
  208. {
  209.     z->m = 1 / a->m;
  210.     z->a = -1 * DTOR(a->a);
  211.     z->a = RTOD(z->a);
  212. }
  213.  
  214. /* Polair SQRT function
  215.  * Z = SQRT(A)
  216.  */
  217. void polsqrt(a, z)
  218. struct polair *a, *z;
  219. {
  220.     z->m = sqrt(a->m);
  221.     z->a = DTOR(a->a) / 2;
  222.     z->a = RTOD(z->a);
  223. }
  224.  
  225. #endif /* _COMPLEXC_ */
  226.